import type { AxiosResponse } from 'axios'
import { takeEvery, put } from 'redux-saga/effects'
import { loginSuccess } from '../actions'
import { IloginAction } from '../actions/action'
import { LOGIN, USERINFO } from '../constants'
import { userLogin, getUserInfo } from '../services/login'

// interface ILoginRes {
//   data: string
// }

// interface ILoginRes {
//   code: number
//   data: string
//   message: string
//   success: boolean
// }

interface IAllApi<T> {
  code: number
  data: T
  message: string
  success: boolean
}

function* login(action: IloginAction): any {
  const response: AxiosResponse<IAllApi<string>> = yield userLogin(action)

  const { data: token } = response.data
  // 将token保存到本地缓存中
  localStorage.setItem('erabbit_token', token)

  yield put(loginSuccess(token))
}

export function* loginTake() {
  yield takeEvery(LOGIN, login)
}
